<?php
namespace home\Controller;

use Think\Controller;

header("Content-type: text/html; charset=utf-8");

class IndexController extends ApibaseController
{
    function __construct() {
        parent::__construct();
    }

    /*
     * 药品信息维护
     */
    public function index() {
        /*参数初始化*/
        $page    = I('get.page', 1);
        $perpage = 10;
        $url     = [];
        $list    = M('Druglist');
        if (I('get.drug_no')) {
            $list           = $list->where(['drug_no' => I('get.drug_no')]);
            $url['drug_no'] = I('get.drug_no');
        }
        if (I('get.drug_name')) {
            $list             = $list->where(['drug_name' => I('get.drug_name')]);
            $url['drug_name'] = I('get.drug_name');
        }

        $copyObject = clone $list;
        $getData    = $list->order(['add_at' => 'desc'])->page($page, $perpage)->select();

        $this->assign('page', setPage($copyObject->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('getData', $getData);
        $this->display('drug/index');
    }

    /**
     * 添加/修改药品种类
     */
    public function addDrugKind(){
        if (I('post.dosubmit')) {
            $data['drug_no']   = I('post.drug_no');
            $data['type']      = I('post.type');
            $data['drug_name'] = I('post.drug_name');
            $data['drug_size'] = I('post.drug_size');
            $data['drug_unit'] = I('post.drug_unit');
            $data['update_at'] = time();
            if (I('post.id')) {
                $update = M('DrugList')->where(['id' => I('post.id')])->setField($data);
                if ($update) {
                    $this->showtips('修改成功');
                }
            }
            $data['add_at'] = time();
            $add            = M('DrugList')->add($data);
            if ($add) {
                $this->showtips('添加成功');
            } else {
                $this->showtips('添加失败');
            }
        } elseif (I('get.id')) {
            $getData = M('DrugList')->where(['id' => I('get.id')])->find();
            $this->assign('getData', $getData);
        }
        $this->display('index/add_drug_kind');
    }

    /**
     * 药品库存查询
     */
    public function stockList(){
        $page    = I('get.page', 1);
        $perpage = I('get.perpage', 10);
        $url     = [];
        $list    = M('Instock')->field("id,drug_no,type,drug_name,drug_size,drug_unit,warehouse,SUM(in_stock) as stock_num");
        if (I('get.drug_no')) {
            $list           = $list->where(['drug_no' => I('get.drug_no')]);
            $url['drug_no'] = I('get.drug_no');
        }
        if (I('get.drug_name')) {
            $list             = $list->where(['drug_name' => I('get.drug_name')]);
            $url['drug_name'] = I('get.drug_name');
        }
        $list = $list->group('drug_name,warehouse');
        $copy = clone $list;
        $getData = $list->select();
        $this->assign('page', setPage($copy->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->assign('getData', $getData);
        $this->display('index/drug_stock');

    }

    /**
     * 药品入库//修改药品
     */

    public function addDrug(){
        if(I('get.dosubmit')){

        }elseif (I('get.id')){
            $getData = M('Instock')->where(['id'=>I('get.id')])->find();
            $this->assign('getData',$getData);
        }
        $this->display('index/add_drug_stock');
    }

    /**
     * 删除入库信息
     */
    public function delDrug(){
        if (I('get.id')) {
            $instockModel  = M('Instock');
            $drugListModel = M('DrugList');
            $drugInfo      = $instockModel->where(['id' => I('get.id')])->find();
            //库存量
            $stock = $drugListModel->where(['drug_no' => $drugInfo['drug_no']])->getField('stock_num');
            //入库量
            $in_stock = $drugInfo['in_stock'];
            if ($tock < $in_stock) {
                $this->showtips('库存不足，删除失败');
            }
            $drugListModel->startTrans();
            $res = $drugListModel->where(['drug_no' => $drugInfo['drug_no']])->setDec('stock_num', $in_stock);
            if ($res) {
                $del = $instockModel->where(['id' => I('get.id')])->delete();
                if ($del) {
                    $drugListModel->commit();
                    $instockModel->commit();
                    $this->showtips('删除成功');
                } else {
                    $drugListModel->rollback();
                    $this->showtips('删除失败');
                }
            } else {
                $this->showtips('删除失败');
            }
        }
    }



}